<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage core
 * @version $Id$
 */

/**	
 * The Default Exception Handler
 * 
 * Exception handler that consumes uncaught framework exceptions and creates and 
 * dumps detailed information inclusive source code parts. Output is genereated as 
 * HTML code.
 *	
 * @package auxo
 * @subpackage core	
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author Andreas Horn, andreas.horn@extronaut.de
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class tx_auxo_DefaultExceptionHandler {

	/**
	 * Defines for how many levels coding details show be dumped if an
	 * exceptions is consumed.
	 */
	const TRACE_CODE_LINES_DEPTH = 1;
	
	/**
	 * Consumes a framework exception and renderes detailed information as HTML/CSS
	 * 
	 * @param exception $exception exception instance
	 */
	static public function consume(exception $exception) {
		$level = 0;
		
		echo '<div style="border: 2px solid grey; padding: 10px;"';
		echo '<h1 >Framework Runtime Exception</h1>';
		echo '<h2 style="color: red;">'. $exception->getMessage() . '</h2>';
		echo '<h3 style="color: blue;font-size:12px;">Uncaught Exception ' . get_class($exception) . ' ';
		echo 'in file ' . $exception->getFile() . ' ';
		echo 'at line ' . $exception->getLine() . ', ';
		echo 'with Code (' . $exception->getCode() . ')</h3>';
        foreach($exception->getTrace() as $trace) {
			echo '<p style="font-size: small;">' . $trace['file'] . '(' . $trace['line']. ') ' .
			     $trace['class'] . $trace['type'] . $trace['function'] . '(';
			
			echo ') </p>';
			
			if ($level++ < self::TRACE_CODE_LINES_DEPTH) {
				self::printCodeSection($trace['file'], $trace['line'], 3, 3);
			}
        }
        echo '</div>';
	}
	
	/**
	 * Prints a PHP code section of a file in a given range
	 * 
	 * Files have to be either given with a fullpath or should be located with 
	 * PHP's include_path.
	 *
	 * @param string $filename filename of PHP source code 
	 * @param int $line a code line
	 * @param int $before number of code lines before given code position
	 * @param int $after number of code lines after given code position
	 */
	static private function printCodeSection($filename, $line, $before, $after) {
		/*
		 * files are read using include_path. 
		 * 
		 * NOTE: file_get_contents parameter $flags has been changed to PHP 6.x and
		 * is not considered here currently
		 */
		if (! ($coding = file_get_contents($filename, true))) {
			return void;
		}
		
		for ($i = $line - $before - 1; $i < $line + $after; $i++) {
			if ($i < 0 || ! $i > count($coding)) {
				continue;
			}
			
			echo '<div style="display:block; color: black; background-color: grey;">';
			echo '<p style="border: 1px solid black; font-size: small;">' . $i . '</p>';
			echo '<p style="font-style: courier; border: 1px solid black; font-size: small;">' . $coding[$i] . '</p>';
			echo '</div>';
		}	
	}
}
?>